import logging
from urllib.parse import quote
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from core.config import get_setting


logger = logging.getLogger("crud")
setting = get_setting()

SQLALCHEMY_DATABASE_URL = (f"mysql+pymysql:"
                           f"//{setting.DB.user}:{quote(setting.DB.password)}@"
                           f"{setting.DB.host}/{setting.DB.database}")

logger.info(f"[database:] SQLALCHEMY_DATABASE_URL={SQLALCHEMY_DATABASE_URL}")

engine = create_engine(
    SQLALCHEMY_DATABASE_URL, encoding='utf-8', pool_recycle=3600, pool_size=20)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()
